package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liu
 * @version 1.0.0
 * @createTime 2020年10月10日
 * @Description 角色控制
 */
@RestController
@RequestMapping("role")
@Slf4j
public class RoleController {

    @Reference
    private RoleService roleService;
    /**
     * 分页查询
     * @return
     */
    @RequestMapping("findPage")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[角色分页查询]data:{}",queryPageBean);
         PageResult page =roleService.findPage(queryPageBean);

    return  new Result(true,"查询成功",page);
}

@RequestMapping("add")
public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
    log.info("[添加角色]role:{},permissionIds:{},menuIds:{}",role,permissionIds,menuIds);

    roleService.add(role,permissionIds,menuIds);

        return  new Result(true,"添加成功");
}
    /**
     * 编辑角色
     * @param role
     * @param permissionIds
     * @param menuIds
     * @return
     */
    @RequestMapping("edit")
public  Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds ){
        log.info("[角色编辑]role:{},permissionIds:{},menuIds:{}",role,permissionIds,menuIds);
           roleService.edit(role,permissionIds,menuIds);

        return new Result(true,"编辑成功");
}
    /**
     * 通过角色id查询权限
     * @param id
     * @return
     */
    @RequestMapping("findPermissionsBypermissionIds")
    public Result findPermissionsBypermissionIds(Integer id){
        log.info("[查询权限  角色id:{}]",id);
        List<Integer> ids =roleService.findPermissionsBypermissionIds(id);

        return new Result(true,"查询成功",ids);
    }
    @RequestMapping("findMenuBymenuIds")
    public Result findMenuBymenuIds(Integer id){
        log.info("[查询菜单  角色id:{}]",id);
        List<Integer> ids =roleService.findMenuBymenuIds(id);

        return new Result(true,"查询成功",ids);
    }
@RequestMapping("findById")
public Result findById(Integer id){

      Role role =roleService.findById(id);

        return new Result(true,"查询成功",role);
}
@RequestMapping("findAll")
public  Result findAll(){
        List<Role> list=roleService.findAll();

        return  new Result(true,"查询所有成功",list);
}
@RequestMapping("findRoleIdsByuserId")
public  Result findRoleIdsByuserId(Integer id){
         List<Integer> list=roleService.findRoleIdsByuserId(id);

        return  new Result(true,"查询成功",list);
}
    @RequestMapping("delete")
    public Result delete(Integer id){
        roleService.delete(id);

        return new Result(true,"删除成功");
    }
}
